#include <my_header.h>
#include "cmd.h"
#define COLOR_DIR "\033[1;34m"
#define COLOR_FILE "\033[0m"    // default color for files
#define COLOR_END "\033[0m"
/* Usage:  */
void normalize_path(const char *raw_path, const char *cwd, char *out_buf, size_t bufsize){
    char temp[2048];
    if(raw_path[0] == '/'){
        strncpy(temp, raw_path, sizeof(temp) - 1);
        temp[sizeof(temp - 1)] = '\0';
    }else{
        snprintf(temp, sizeof(temp), "%s/%s", cwd, raw_path);
    }
    char *parts[512];
    int top = 0;
    char *token = strtok(temp, "/");
    while(token){
        if(strcmp(token, "") == 0 || strcmp(token, ".") == 0){
            //跳过
        }else if(strcmp(token, "..") == 0){
            if(top > 0){
                top--;
            }
        }else{
            parts[top] = token;
            top++;
        }
        token = strtok(NULL, "/");
    }
    char *p = out_buf;
    size_t remain = bufsize;
    if(top == 0){
        snprintf(out_buf, bufsize, "/");
        return;
    }
    for(int i = 0;i < top;i++){
        int n = snprintf(p, remain, "/%s", parts[i]);
        p += n;
        remain -= n;
        if(remain <= 0){
            break;
        }
    }
}

int ls(MYSQL *db, my_stack *pstack, char *output_buf, size_t bufsize, const char *input_path, const char *username){
    /* char error_buf[1024]; */
    char path_buf[1024];
    char sql[2000];
    int dir_id;
    memset(output_buf, 0, bufsize);
    if(input_path){
        char cwd[1024];
        if(pwd(db, pstack, cwd, sizeof(cwd)) != 0){
            snprintf(output_buf, bufsize, "pwd");
            return -1;
        }
        normalize_path(input_path, cwd, path_buf, sizeof(path_buf));
    }else{
        if(pwd(db, pstack, path_buf, sizeof(path_buf)) != 0){
            snprintf(output_buf, bufsize, "pwd");
            return -1;
        }
    }
    snprintf(sql, sizeof(sql), "select id,type from virtual_directory where path = '%s' and user_name = '%s' and is_deleted = 0", path_buf, username);
    if(mysql_query(db, sql)){
        snprintf(output_buf, bufsize, "mysql error:%s", mysql_error(db));
        return -1;
    }
    MYSQL_RES *res = mysql_store_result(db);
    if(!res){
        snprintf(output_buf, bufsize, "mysql_store_result error:%s", mysql_error(db));
        mysql_free_result(res);
        return -1;
    }
    MYSQL_ROW row = mysql_fetch_row(res);
    if(!row){
        snprintf(output_buf, bufsize, "ls:path '%s' not found for user:'%s'", path_buf, username);
        mysql_free_result(res);
        return -1;
    }
    int target_id = atoi(row[0]);
    const char *type = row[1];
    mysql_free_result(res);
    if(strcmp(type, "f") == 0){
        const char *base = strrchr(path_buf, '/');
        printf(COLOR_FILE "%s" COLOR_END "\n", base + 1);
        return 0;
    }
    dir_id = target_id;
    snprintf(sql, sizeof(sql), "select file_name, type from virtual_directory where parent_id = %d and user_name = '%s' and is_deleted =0", dir_id, username);
    if(mysql_query(db, sql)){
        snprintf(output_buf, bufsize, "mysql error:%s", mysql_error(db));
        return -1;
    }
    res = mysql_store_result(db);
    if(!res){
        snprintf(output_buf, bufsize, "mysql_store_result error:%s", mysql_error(db));
        return -1;
    }
    size_t offset = 0;
    while((row = mysql_fetch_row(res)) != NULL){
        const char *file_name = row[0];
        const char *type = row[1];
        int written;
        if(strcmp(type, "d") == 0){
            written = snprintf(output_buf + offset, bufsize - offset, COLOR_DIR "%s" COLOR_END "  ", file_name);
        }else{
            written = snprintf(output_buf + offset, bufsize - offset, COLOR_FILE "%s" COLOR_END "  ", file_name);
        }
        if(written < 0 || written >= bufsize - offset){
            break;
        }
        offset += written;
    }
    printf("\n");
    mysql_free_result(res);
    return 0;
}

int remove_file(MYSQL *db, my_stack *pstack, char *buf, size_t bufsize, const char *filename, const char *username){
    char path_buf[1024];
    char sql[2000];
    char cwd[1024];
    if(pwd(db, pstack, cwd, sizeof(cwd)) != 0){
        snprintf(buf, bufsize, "pwd");
        return -1;
    }
    normalize_path(filename, cwd, path_buf, sizeof(path_buf));
    snprintf(sql, sizeof(sql), "select id, type from virtual_directory where path = '%s' and user_name = '%s' and is_deleted = 0", path_buf, username);
    if(mysql_query(db, sql)){
        snprintf(buf, bufsize, "mysql error:%s", mysql_error(db));
        return -1;
    }
    MYSQL_RES *res = mysql_store_result(db);
    if(!res){
        snprintf(buf, bufsize, "mysql_store_result error:%s", mysql_error(db));
        mysql_free_result(res);
        return -1;
    }
    MYSQL_ROW row = mysql_fetch_row(res);
    if(!row){
        snprintf(buf, bufsize, "remove:filename '%s' not found for user:%s", path_buf, username);
        mysql_free_result(res);
        return -1;
    }
    /* int target_id = atoi(row[0]); */
    const char *type =row[1];
    mysql_free_result(res);
    if(strcmp(type, "d") == 0){
        snprintf(buf, bufsize, "it's not a file");
        return -1;
    }
    snprintf(sql, sizeof(sql), "update virtual_directory set is_deleted = 1 where path = '%s' and user_name = '%s'", path_buf, username);
    if(mysql_query(db, sql)){
        snprintf(buf, bufsize, "mysql error:%s", mysql_error(db));
        return -1;
    }
    return 0;
}

